from ctypes import sizeof
from .dsdiffdef import * 

#def CopyBytes(ffin, ffout, size):
#	buf = ffin.read(size)
#	ffout.write(buf)

#def modify_chunk_size(ffout, oldHead, newSize):
#	buf = struct.pack(">IQ", oldHead[0], newSize)
#	ffout.write(buf)

#def getSomeChunkInfo(ff, id):
#    header = ""
#    offset = -1
#    while(True):
#        header = GetChunkInfo(ff)
#        id1 = header[0].to_bytes(4, byteorder="big", signed=False)
#        if(id1 == id):
#            offset = ff.tell()
#            print("Find ", id, "chunk at offset: ", offset)
#            break
#        else:
#            ff.seek(header[1], 1)
#    return header, offset

#def getPROPChunkInfo(ff, id):
#    return getSomeChunkInfo(ff, b'PROP')

#def GetChunkInfo(ff):
#    try:
#        chunk = ff.read(12)
#        print(chunk)
#        ivar = struct.unpack(">IQ",chunk)
#    except EOFError:
#        ivar = ""	
#    return ivar

#def getDSDFormChunkInfo(ff):
#	
#    ivar = GetChunkInfo(ff)
#    if(ivar != ""):    
#        id = ivar[0].to_bytes(4, byteorder="big", signed=False)
#        if(id == b'FRM8'):
#            return ivar
#    print("Error, not a DSDIFF file!")
#    return ivar

#typedef ID3_FRAME {
#    uint8_t frame_id[4];
#    uint32_t frame_size;
#    uint16_t frame_flags;
#} ID3_FRAME_t;
#ID3FrameHeaderFmt = '>4BIH'
#ID3FrameHeaderSize = struct.calcsize(ID3FrameHeaderFmt)
def new_ID3_text_tag(singleTag,type):
    str1 = singleTag[type]
    flags = 0
    strSize = len(str1)    
    if(type == b'TALB' or type == b'TPE1' or type == b'TIT2'):
        encodeing = 1
        buf = encodeing.to_bytes(1, 'big') + bytes(str1, 'utf-16') + bytes(1)        
        strSize = len(buf)
        buf = struct.pack('>4sIH', type, strSize, flags) + buf

    else:
        encodeing = 0
        buf = encodeing.to_bytes(1, 'big') + bytes(str1, 'utf-8') 
        strSize = len(buf)
        buf = struct.pack('>4sIH', type, strSize, flags) + buf 
#    print(buf)
    return buf

#typedef ID3_HEADER {
#    uint8_t id3_identifier[3]; 头部标识。占3个字节，由字符ID3组成，表示这是一个ID3v2的标签; 
#    uint8_t id3_version; 主版本号。版本号 ID3V2.3 就记录 3
#    uint8_t id3_revision;
#    uint8_t id3_flags;
#    uint32_t id3_size;
#} ID3_HEADER_t;
#ID3ChunkHeaderFmt = '>IQ'
#ID3ChunkHeaderSize = struct.calcsize(ID3ChunkHeaderFmt)
#ID3TagHeaderFmt = '>3BBBBI'
#ID3TagHeaderSize = struct.calcsize(ID3TagHeaderFmt)

#Header　　3    ID3V2.3标识符"ID3"的Ascii码，否则认为没有ID3V2.3
#Ver　　　 1　　版本号，＝03
#Revision　1　　副版本号，＝00
#flag　　　1　　标志字节，一般没意义，＝00
#Size　　　4　　标签内容长度，高位在前，不包括标签头的10个字节
#---------------------------------------------------------------------

#备注：

#1、Size：Size的计算公式如下：
#使用位与 |：
#
#size = ((Size[0] & 0x7f) << 21) | 
#
#((Size[1] & 0x7f) << 14) |  
#
#((Size[2] & 0x7f) << 7) |
#
# (Size[3] & 0x7f);
def convert_ID3_size_from_int(id3Size):
    a0 = id3Size & 0x7f
    a1 = id3Size>>7 & 0x7f
    a2 = id3Size>>14 & 0x7f
    a3 = id3Size>>21 & 0x7f

    return struct.pack('>4B', a3,a2,a1,a0)

def convert_ID3_size_to_int(id3Size):
    a =  struct.unpack('>4B', id3Size)
    a0 = (a[0] & 0x7f) << 21
    a1 = (a[1] & 0x7f) << 14
    a2 = (a[2] & 0x7f) << 7
    a3 = a[3] & 0x7f

    return a0+a1+a2+a3

def new_ID3_tag(singleTag):

    frames = new_ID3_text_tag(singleTag, b'TALB') + \
        new_ID3_text_tag(singleTag, b'TPE1') + \
        new_ID3_text_tag(singleTag, b'TYER') + \
        new_ID3_text_tag(singleTag, b'TIT2') + \
        new_ID3_text_tag(singleTag, b'TRCK')

    id3Size = convert_ID3_size_from_int(len(frames))

    header = struct.pack('>3sBBB4s', b'ID3',3,0,0,id3Size)

    padnum = len(frames) % 4
    
    return header+frames+bytes(padnum)
